
from feature_data.const_data import *
from feature_data.XmlData2DictData import Xml2Dict

from feature_data.Dict2Venue_Graphfeature import Feature_venue_extration
from feature_data.answer_extration import Answer_extration

from nd_utils.Graph_Cluster_util import *
from nd_utils.networkx_util import *


class Graph_venue_stage():
    def __init__(self, feature_venue_extration, answer_extration):

        self.feature_venue_extration = feature_venue_extration

        self.init_graph = self.feature_venue_extration.init_graph
        self.cluster_covenue_pair_set_list = self.feature_venue_extration.cluster_covenue_pair_set_list

        self.third_cluster_graph = self._get_cluster_graph()
        self.true_cluster_num = answer_extration.true_cluster_num
        self.true_paperidx_cluster_list = answer_extration.true_paperidx_cluster_list

        self.simulation_occur_flag = self._get_simulation_occur_flag()

        self.subgraph_list = self._get_subgraph_list()
        self.previous_subgraph_nodes_list = self._get_third_subgraph_nodes_list()

    def _get_cluster_graph(self):
        pair_set_list = self.cluster_covenue_pair_set_list
        cluster_graph = add_pair_set_to_graph(pair_set_list, deepcopy_graph(self.init_graph),_weight=1.5  )
        return cluster_graph

    def _get_simulation_occur_flag(self):
        predict_cluster_num = get_subgraph_num(self.third_cluster_graph)
        if predict_cluster_num >= self.true_cluster_num:
            return True
        else:
            return False

    def _get_subgraph_list(self):
        if self.simulation_occur_flag:
            return get_subgraphs(self.third_cluster_graph)
        else:
            return get_subgraphs(self.init_graph)

    def _get_third_subgraph_nodes_list(self):
        return get_subgraph_nodes_list(self.subgraph_list)

def print_clust_total(cluster_list):
    paper_pair_set_list = cluster_list
    sum = 0

    union_set = []

    set_length_list = []
    for paper_pair_set in paper_pair_set_list:
        # print list(paper_pair_set), len(paper_pair_set)
        print sorted(list(paper_pair_set)), len(paper_pair_set)
        set_length_list.append(len(paper_pair_set))
        sum += len(paper_pair_set)

        union_set.extend(list(paper_pair_set))
    print "sum: ", sum
    # print sorted(list(union_set)), len(list(union_set))
    print sorted(list(set(union_set))) , len(set(union_set))
    print set_length_list

from nd_utils.trans_clusteridxlist_paperidx_pairset import get_paperidx_pairset_list
from model_metric import Model_metric

if __name__ == '__main__':

    # for xml_file_path in xml_filepath_list:
    for xml_file_path in xml_file_path_list:
        # xml2dict = Xml2Dict(xml_dir + xml_file_name2) # 0.656171284635 0.560215053763 0.604408352668
        xml2dict = Xml2Dict(xml_file_path) # 0.656171284635 0.560215053763 0.604408352668
        feature_venue_extration = Feature_venue_extration(xml2dict)
        answer_extration = Answer_extration(xml2dict)
        third_stage_clust = Graph_venue_stage(feature_venue_extration, answer_extration)

        # print first_stage_clust.cluster_list
        # print_clust_total(third_stage_clust.subgraph_list)

        print third_stage_clust.simulation_occur_flag

        # print_subgraphs(first_stage_clust)
        from nd_utils.matplotlib_helper import *

        # plt_networkx_main_third(third_stage_clust.third_cluster_graph)

        paperid_pairset_list = get_paperidx_pairset_list(third_stage_clust.true_paperidx_cluster_list)

        predict_paperid_pairset_list = get_paperidx_pairset_list(third_stage_clust.previous_subgraph_nodes_list)

        model_metric = Model_metric(paperid_pairset_list, predict_paperid_pairset_list )

        print model_metric.pairwise_precision, model_metric.pairwise_recall, model_metric.pairwise_f1

"""
False
0.860613810742 0.741189427313 0.796449704142
True
0.656171284635 0.560215053763 0.604408352668
True
1.0 0.961636828645 0.980443285528
False
0.562266167825 0.881072026801 0.686460032626
False
1.0 0.773838630807 0.87250172295
True
0.309921581998 0.769686706181 0.441905687895
False
1.0 0.933985330073 0.965865992415
False
0.995412844037 0.612994350282 0.758741258741
True
0.995433789954 0.986425339367 0.990909090909
True
0.827517447657 0.930493273543 0.87598944591

"""